package Model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.JFrame;

import Events.GenericEvent;
import Events.MoodleEvent;
import Events.Appointment;

public class DAOObjectIndex {
	
	ArrayList<Index> actualMonth;
	private String EXTENTION = ".ejd";
	private String DIRECTION = "docs\\";
	int acurrentMonth;
	public DAOObjectIndex(ArrayList<Index> actualMonth)
	{
		this.actualMonth=actualMonth;
		
	}
	public boolean SaveIndexMonth(GenericEvent event, ArrayList<Index> actualMonth, boolean month)
	{
		try {
			ArrayList<Index> monthIndex = new ArrayList<Index>();
			boolean entro=false;
			if(month)
				monthIndex =actualMonth;
			else
				monthIndex = getMonth(2, event.getSaveTime());
			//Crear el indice y ponerle el indice2 del moodle
			Index in = new Index(event.getTime_Start(), event.getIdEvent());
			if(event instanceof Appointment)
			{
				
				Appointment eve = (Appointment)event;
				if(eve.getParams().get("IDME")!=null)
					in.setIndex2(eve.getParams().get("IDME").toString());
			}
			
			long time = event.getTime_Start();
			for(int i=0; i<monthIndex.size(); i++)
			{
				if(monthIndex.get(i).getTime()>time)
				{
					entro=true;
					monthIndex.add(i, in);
					break;
				}
			}
			if(!entro)
			{
				monthIndex.add(in);
			}
			//int def = event.getD_Start().getYear();
			acurrentMonth=event.getSaveTime();
			ObjectOutputStream ob = new ObjectOutputStream(new FileOutputStream(new File(DIRECTION+acurrentMonth+EXTENTION)));
			ob.writeObject(monthIndex);
			ob.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
//	public int getMontNumber()
//	{
//		//return (actualMonth==null ? new Date() : new Date(actualMonth.get(0).getTime())).getMonth()+actualYear;
//		
//	}
	
	public ArrayList<Index> getMonth(int number, int accmnth)
	{
		acurrentMonth=accmnth;
		ObjectInputStream ob;
		ArrayList<Index> month = null;
		try{
			if(number==0)
			{
				Date d = new Date();
				//acurrentMonth= d.getYear()*d.getMonth();
				File f = new File(DIRECTION+acurrentMonth+EXTENTION);
				ob = new ObjectInputStream(new FileInputStream(f));
				
			}
			else if(number==1 || number==-1)
			{
				ob = new ObjectInputStream(new FileInputStream(new File(DIRECTION+(acurrentMonth+number)+EXTENTION)));
				//month = (ArrayList<Index>)ob.readObject();
				//acurrentMonth+=number;
			}
			else 
			{
				ob = new ObjectInputStream(new FileInputStream(new File(DIRECTION+number+EXTENTION)));
				//month = (ArrayList<Index>)ob.readObject();
				//acurrentMonth=number;
			}
			month = (ArrayList<Index>)ob.readObject();
			ob.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			month = new ArrayList<Index>();
			System.out.println("Month not found");
			//e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return month;
	}
}
